// c-api-examples/add-punctuation-c-api.c
//
// Copyright (c)  2024  Xiaomi Corporation

// We assume you have pre-downloaded the model files for testing
// from https://github.com/k2-fsa/sherpa-onnx/releases/tag/punctuation-models
//
// An example is given below:
//
// clang-format off
//
// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
// tar xvf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
// rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
//
// clang-format on

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#include "sherpa-onnx/c-api/c-api.h"
 
int32_t main() {
  // 初始化标点符号预测配置结构体
  SherpaOnnxOfflinePunctuationConfig config;
  memset(&config, 0, sizeof(config));  // 将配置结构体内存清零
 
  // clang-format off
  // 设置模型路径（中文-英文标点预测模型）
  config.model.ct_transformer = "./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx";
  // clang-format on
  
  // 配置模型参数
  config.model.num_threads = 1;      // 使用1个线程
  config.model.debug = 1;            // 启用调试模式
  config.model.provider = "cpu";     // 使用CPU进行推理
 
  // 创建标点符号预测器实例
  const SherpaOnnxOfflinePunctuation *punct =
      SherpaOnnxCreateOfflinePunctuation(&config);
  if (!punct) {
    // 创建失败时打印错误信息并退出
    fprintf(stderr,
            "Failed to create OfflinePunctuation. Please check your config");
    return -1;
  }
 
  // 测试文本数组（包含中英文混合句子）
  const char *texts[] = {
      "这是一个测试你好吗How are you我很好thank you are you ok谢谢你",
      "我们都是木头人不会说话不会动",
      ("The African blogosphere is rapidly expanding bringing more voices "
       "online in the form of commentaries opinions analyses rants and poetry"),
  };
 
  // 计算测试文本数量
  int32_t n = sizeof(texts) / sizeof(const char *);
  fprintf(stderr, "n: %d\n", n);  // 打印文本数量
 
  fprintf(stderr, "--------------------\n");
  // 遍历处理每条测试文本
  for (int32_t i = 0; i != n; ++i) {
    // 调用API添加标点符号
    const char *text_with_punct =
        SherpaOfflinePunctuationAddPunct(punct, texts[i]);
 
    // 打印输入输出对比
    fprintf(stderr, "Input text: %s\n", texts[i]);
    fprintf(stderr, "Output text: %s\n", text_with_punct);
    
    // 释放API返回的字符串内存
    SherpaOfflinePunctuationFreeText(text_with_punct);
    fprintf(stderr, "--------------------\n");
  }
 
  // 销毁标点符号预测器实例
  SherpaOnnxDestroyOfflinePunctuation(punct);
 
  return 0;
}

